﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ST.Core.Util
{
    public static class ByteUtil
    {
        /// <summary>
        /// 判断两个字节数组是否相等
        /// </summary>
        /// <param name="bytes1">第一个字节数组</param>
        /// <param name="bytes2">第二个字节数组</param>
        /// <returns>如果两个数组相等返回true，否则返回false</returns>
        public static bool AreEqual(byte[] bytes1, byte[] bytes2)
        {
            if (bytes1 == null || bytes2 == null)
                return bytes1 == bytes2;

            if (bytes1.Length != bytes2.Length)
                return false;

            for (int i = 0; i < bytes1.Length; i++)
            {
                if (bytes1[i] != bytes2[i])
                    return false;
            }

            return true;
        }

        /// <summary>
        /// 将字节数组转换为16进制字符串，每个字节用空格分隔
        /// </summary>
        /// <param name="bytes">要转换的字节数组</param>
        /// <returns>16进制字符串</returns>
        public static string ToHexString(byte[] bytes)
        {
            if (bytes == null || bytes.Length == 0)
                return string.Empty;

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < bytes.Length; i++)
            {
                sb.Append(bytes[i].ToString("X2"));
                if (i < bytes.Length - 1)
                {
                    sb.Append(" ");
                }
            }
            return sb.ToString();
        }

        /// <summary>
        /// 将16进制字符串转换为字节数组
        /// </summary>
        /// <param name="hexString">16进制字符串，可以包含空格分隔符</param>
        /// <returns>转换后的字节数组</returns>
        public static byte[] HexStringToBytes(string hexString)
        {
            if (string.IsNullOrEmpty(hexString))
                return new byte[0];

            // 去除所有空格
            hexString = hexString.Replace(" ", "");

            // 检查字符串长度是否为偶数
            if (hexString.Length % 2 != 0)
                throw new ArgumentException("16进制字符串长度必须为偶数");

            byte[] bytes = new byte[hexString.Length / 2];
            for (int i = 0; i < bytes.Length; i++)
            {
                string byteString = hexString.Substring(i * 2, 2);
                bytes[i] = Convert.ToByte(byteString, 16);
            }

            return bytes;
        }



        public static byte[] CalculateModbusCRC16(byte[] data, int begin, int len)
        {
            // 初始化 CRC 寄存器为 0xFFFF
            ushort crc = 0xFFFF;

            // 循环处理每个字节
            for (int i = begin; i < begin + len; i++)
            {
                crc ^= data[i];

                // 对每个字节的 8 位进行处理
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 0x0001) == 1)
                    {
                        crc = (ushort)((crc >> 1) ^ 0xA001);
                    }
                    else
                    {
                        crc >>= 1;
                    }
                }
            }

            // 将 ushort 类型的 CRC 值转换为 byte 数组
            byte[] crcBytes = new byte[2];
            crcBytes[0] = (byte)(crc & 0xFF);
            crcBytes[1] = (byte)(crc >> 8);

            return crcBytes;
        }
    }
 

}
